# Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
# reserved. Use of this source code is governed by a BSD-style license that
# can be found in the LICENSE file.

#
# Source files.
#
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# cefsimple sources.
file(GLOB_RECURSE SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c*
)

file(GLOB_RECURSE HEADERS
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h*
)

file(GLOB_RECURSE RESOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.rc
)
# Set C++20 standard (must be set before adding targets)

# Target executable names.
set(CEF_TARGET "audio_player")

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../ffmpeg_wrapper/src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/taglib/include/taglib)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib/include)

# Logical target used to link the libcef library.
ADD_LOGICAL_TARGET("libcef_lib" "${CEF_LIB_DEBUG}" "${CEF_LIB_RELEASE}")

# Add ffmpeg_wrapper include directory


link_directories(${FFMPEG_LIB_DIR})

# Logical target used to link the ffmpeg_wrapper library
# Set paths for debug and release versions of the library using CMAKE_BINARY_DIR to get build directory
set(FFMPEG_WRAPPER_LIB_DEBUG "${CMAKE_BINARY_DIR}/ffmpeg_wrapper/Debug/ffmpeg_wrapper.lib")
set(FFMPEG_WRAPPER_LIB_RELEASE "${CMAKE_BINARY_DIR}/ffmpeg_wrapper/Release/ffmpeg_wrapper.lib")

# Create a logical target for the ffmpeg_wrapper library
ADD_LOGICAL_TARGET("ffmpeg_wrapper_lib" "${FFMPEG_WRAPPER_LIB_DEBUG}" "${FFMPEG_WRAPPER_LIB_RELEASE}")

# Add glog library support
set(GLOG_LIB_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/../logger/lib/glogd.lib")
set(GLOG_LIB_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/../logger/lib/glog.lib")
ADD_LOGICAL_TARGET("glog_lib" "${GLOG_LIB_DEBUG}" "${GLOG_LIB_RELEASE}")

set(TAG_LIB_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/third_party/taglib/lib/tagd.lib")
set(TAG_LIB_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/third_party/taglib/lib/tag.lib")
ADD_LOGICAL_TARGET("tag_lib" "${TAG_LIB_DEBUG}" "${TAG_LIB_RELEASE}")

set(ZLIB_LIB_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib/lib/zlibstaticd.lib")
set(ZLIB_LIB_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib/lib/zlibstatic.lib")
ADD_LOGICAL_TARGET("zlib_lib" "${ZLIB_LIB_DEBUG}" "${ZLIB_LIB_RELEASE}")

# Determine the target output directory.
SET_CEF_TARGET_OUT_DIR()

if(USE_SANDBOX)
  # Shared library (DLL) target.
  add_library(${CEF_TARGET} SHARED ${SRCS} ${HEADERS} ${RESOURCES})
  SET_LIBRARY_TARGET_PROPERTIES(${CEF_TARGET})

  # Copy and rename the bootstrap binary.
  COPY_SINGLE_FILE(${CEF_TARGET} ${CEF_BINARY_DIR}/bootstrap.exe ${CEF_TARGET_OUT_DIR}/${CEF_TARGET}.exe)

  if(CMAKE_GENERATOR MATCHES "Visual Studio")
    # Make the bootstrap binary the default command for the Visual Studio debugger.
    set_target_properties(${CEF_TARGET} PROPERTIES
      VS_DEBUGGER_COMMAND "$<TARGET_FILE_DIR:${CEF_TARGET}>/${CEF_TARGET}.exe"
    )
  endif()
else()
  # Executable target.
  add_executable(${CEF_TARGET} WIN32 ${SRCS} ${HEADERS} ${RESOURCES})
  SET_EXECUTABLE_TARGET_PROPERTIES(${CEF_TARGET})

  # Add the custom manifest files to the executable.
  # ADD_WINDOWS_MANIFEST("${CMAKE_CURRENT_SOURCE_DIR}/win" "${CEF_TARGET}" "exe")
endif()

# 添加编译器标志来禁用特定警告
if(MSVC)
  # 禁用警告：C4819(字符编码问题)、C4005(宏重定义)
  target_compile_options(${CEF_TARGET} PRIVATE
    /wd4819
    /wd4005
    /std:c++20
  )
  target_compile_definitions(${CEF_TARGET} PRIVATE TAGLIB_STATIC ZLIB_STATIC)
endif()

set(FFMPEG_LIBS
    avcodec
    avformat
    avutil
    swscale
    swresample
)

add_dependencies(${CEF_TARGET} libcef_dll_wrapper ffmpeg_wrapper logger)
target_link_libraries(${CEF_TARGET} ${FFMPEG_LIBS} libcef_lib libcef_dll_wrapper ffmpeg_wrapper_lib glog_lib tag_lib zlib_lib ${CEF_STANDARD_LIBS})

# Copy binary and resource files to the target output directory.
COPY_FILES("${CEF_TARGET}" "${CEF_BINARY_FILES}" "${CEF_BINARY_DIR}" "${CEF_TARGET_OUT_DIR}")
COPY_FILES("${CEF_TARGET}" "${CEF_RESOURCE_FILES}" "${CEF_RESOURCE_DIR}" "${CEF_TARGET_OUT_DIR}")

if(USE_SANDBOX)
  # Set LPAC ACLs required for Windows sandbox support.
  SET_LPAC_ACLS("${CEF_TARGET}")
endif()

# Define FFmpeg binary files
set(FFMPEG_BINARY_FILES
    avcodec.dll
    avdevice.dll
    avfilter.dll
    avformat.dll
    avutil.dll
    postproc.dll
    swresample.dll
    swscale.dll
)

# Copy FFmpeg DLLs to the target executable output directory
COPY_FILES("${CEF_TARGET}" "${FFMPEG_BINARY_FILES}" "${FFMPEG_ROOT}/bin/x64" "${CEF_TARGET_OUT_DIR}")